﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.IO;
using System.Collections;
using System.Runtime.Serialization.Formatters.Binary;
namespace WindowsFormsApplication2
{
    
    public partial class Form1 : Form
    {
        private ArrayList ds;
        private int n;
        private string chuoigio;
        private Shape shape;
        private Size szMove = new Size(5, 5);
        private bool isMove = false;
        public Form1()
        {
            InitializeComponent();
            // Xay dung menu con cua mnuOptionpenstyle
           
            // Anh xa su kien ve hinh
            this.Paint += new PaintEventHandler(Form1_Paint);
            this.MouseDown += new MouseEventHandler(Form1_MouseDown);
            //Khoi tao gia tri ban dau cho hinh
            shape = new Shape(new Point(ClientSize.Width / 2, ClientSize.Width / 2));
            //cap chuoi gio
            DateTime dt = DateTime.Now;
            chuoigio = dt.ToString("hh:mm:ss");
            //Khoi dong Timer
            timer1.Start();
            //khoi tao n
            n = 0;
        }

        void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {

                shape.PointShape = new Point(e.X, e.Y);
                Invalidate();
            }
            if (e.Button == MouseButtons.Right)
            {
                if (n == 5)
                {
                    n = 0;
                    MessageBox.Show(n.ToString());
                    shape.PenStyle = DashStyle.Custom;
                    Invalidate();

                }
                if (n == 4)
                {
                    n++;
                    MessageBox.Show(n.ToString());
                    shape.PenStyle = DashStyle.DashDotDot;
                    Invalidate();

                }
                if (n == 3)
                {
                    n++;
                    MessageBox.Show(n.ToString());
                    shape.PenStyle = DashStyle.DashDot;
                    Invalidate();

                }
                if (n == 2)
                {
                    n++;
                    MessageBox.Show(n.ToString());
                    shape.PenStyle = DashStyle.Dot;
                    Invalidate();

                }
                if (n == 1)
                {
                    n++;
                    MessageBox.Show(n.ToString());
                    shape.PenStyle = DashStyle.Solid;
                    Invalidate();

                }
                if (n == 0) {
                    n++;
                    MessageBox.Show(n.ToString());
                    shape.PenStyle = DashStyle.Dash;
                    Invalidate();
 
                }
                
             
               
               
               
            }
        }
        void Form1_Paint(object sender, PaintEventArgs e)
        {
            foreach (Shape a in ds)
            {
                a.DrawString(e.Graphics, chuoigio);
            }
        }

        private void Form1_ClientSizeChanged(object sender, EventArgs e)
        {
            // Cap nhat lai toa do
            shape.PointShape = new Point(ClientSize.Width / 2, ClientSize.Height / 2);
            // Yeu cau ve lai
            Invalidate();
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            { 
                case Keys.Up:
                    shape.PointShape = new Point(shape.PointShape.X, shape.PointShape.Y - 5);
                    break;
                case Keys.Down:
                    shape.PointShape = new Point(shape.PointShape.X, shape.PointShape.Y + 5);
                    break;
                case Keys.Left:
                    shape.PointShape = new Point(shape.PointShape.X - 5,shape.PointShape.Y);
                    break;
                case Keys.Right:
                    shape.PointShape = new Point(shape.PointShape.X + 5,shape.PointShape.Y);
                    break;

            }
            Invalidate();
        }

        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            switch (e.KeyChar)
            { 
                case '+':
                    shape.FontSize += 1;
                    break;
                case '-':
                    shape.FontSize -=1;
                    break;
                   
            }
            Invalidate();
        }

        private void mnuOptionColorCircle_Click(object sender, EventArgs e)
        {
            // Tao Color Dialog
            ColorDialog dlg = new ColorDialog();
            // Gan thuoc tinh cho Dialog
            dlg.Color = shape.ShapeFont;
            // Hien thi va lay ket qua 
            DialogResult ret = dlg.ShowDialog();
            // XU ly ket qua
            if (ret == System.Windows.Forms.DialogResult.OK)
            {
               shape.ShapeFont = dlg.Color;
                Invalidate();
            }//if
        }

        private void mnuOptionColorRect_Click(object sender, EventArgs e)
        {
            // Tao Color Dialog
            ColorDialog dlg = new ColorDialog();
            // Gan thuoc tinh cho Dialog
            dlg.Color = shape.PenColor;
            // Hien thi va lay ket qua 
            DialogResult ret = dlg.ShowDialog();
            // XU ly ket qua
            if (ret == System.Windows.Forms.DialogResult.OK)
            {
                shape.PenColor = dlg.Color;
                Invalidate();
            }//if
        }

        
        private void mnuOptionWidth_Click(object sender, EventArgs e)
        {
            // Hien thi Dialog
            FormWidth dlg = new FormWidth();
            // Thiet lap tinh chat
            dlg.FontSize = shape.FontSize;
            // Hien thi va layk et qua
            DialogResult ret = dlg.ShowDialog();
            // Xu ly
            if(ret == System.Windows.Forms.DialogResult.Yes)
            {
                shape.FontSize = dlg.FontSize;
                Invalidate();
            }//if
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //cap chuoi gio
            DateTime dt = DateTime.Now;
            chuoigio = dt.ToString("hh:mm:ss");
          
            //Neu ismove = false thi ko xu ly (thoat ham)
            if (isMove == false) return;
            //Dung bien phai/trai
            if(shape.PointShape.X + szMove.Width < 0
                || shape.PointShape.X  + szMove.Width>ClientSize.Width)
            {
                szMove.Width = -szMove.Width;
            }
            if (shape.PointShape.Y + szMove.Height < 0
               || shape.PointShape.Y + szMove.Height > ClientSize.Height)
            {
                szMove.Height = -szMove.Height;
            }
          
            shape.PointShape = new Point(shape.PointShape.X + szMove.Width, shape.PointShape.Y + szMove.Height);
            Invalidate();
           
        }


        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            timer1.Stop();

        }

        private void mnOptionMove_Click(object sender, EventArgs e)
        {
            isMove = !isMove;
        }

        private void mnuoptionpenstyle_Click(object sender, EventArgs e)
        {

        }

        private void solidToolStripMenuItem_Click(object sender, EventArgs e)
        {
            shape.PenStyle = DashStyle.Solid;
            Invalidate();
        }

        private void dashToolStripMenuItem_Click(object sender, EventArgs e)
        {
            shape.PenStyle = DashStyle.Dash;
            Invalidate();
        }

        private void dotToolStripMenuItem_Click(object sender, EventArgs e)
        {
            shape.PenStyle = DashStyle.Dot;
            Invalidate();

        }

        private void dashDotToolStripMenuItem_Click(object sender, EventArgs e)
        {
            shape.PenStyle = DashStyle.DashDot;
            Invalidate();
        }

        private void dashDotDotToolStripMenuItem_Click(object sender, EventArgs e)
        {
            shape.PenStyle = DashStyle.DashDotDot;
            Invalidate();
        }

        private void customToolStripMenuItem_Click(object sender, EventArgs e)
        {
            shape.PenStyle = DashStyle.Custom;
            Invalidate();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ds = new ArrayList();
            ds.Add(shape);
            Invalidate();
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "Text|*.txt|File Out|*.out|All File|*.*";

            if (dlg.ShowDialog() == DialogResult.OK) {
                FileStream f = new FileStream(dlg.FileName, FileMode.Create);
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(f, ds); 
                f.Close();
            
            
            }
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "Text|*.txt|File Out|*.out|All File|*.*";

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                FileStream f = new FileStream(dlg.FileName, FileMode.Open);
                BinaryFormatter bf = new BinaryFormatter();
               ds=(ArrayList) bf.Deserialize(f);
                f.Close();

                Invalidate();
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }



        

     
    }
}
